<?php

namespace App\Library\Service;

use App\Library\Logic\CartLogic;
use App\Library\Logic\ShopOrderLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Logic\ProductSpecsLogic;
use App\Library\Requests\Base\SerialNumbersRequest;
use App\Library\Requests\Cart\CartAddRequest;
use App\Library\Requests\Cart\CartEditRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;

class CartService extends BaseService
{
    /**
     * logic
     * @return CartLogic
     */
    private function logic(): CartLogic
    {
        if (empty($this->logic)) $this->logic = new CartLogic;
        return $this->logic;
    }

    /**
     * 列表
     * @return JsonResponse
     */
    public function list(): JsonResponse
    {
        return success($this->logic()->list());
    }

    /**
     * 添加
     * @param CartAddRequest $request
     * @return JsonResponse
     */
    public function add(CartAddRequest $request): JsonResponse
    {
        $product = ShopProductLogic::findBySerialNumber($request);
        $specsInfo = ProductSpecsLogic::findByWhere(['ps_id' => $request->specId]);

        if (is_null($product)) return fail(433);
        if ($product->product_id !== $specsInfo->product_id) return fail(433);
        if ($specsInfo->ps_is_delete === 1) return fail(433);
        else if ($specsInfo->ps_usable_stock < $request->quantity) return fail(625);
        //目标商品过往购买数量
        $orderCount = ShopOrderLogic::countBoughtProduct($product->product_id, $specsInfo->ps_id);
        if ($specsInfo->ps_buy_limit > 0) {
            if ($specsInfo->ps_buy_limit < bcadd($orderCount, $request->quantity)) return fail(624);
        }

        $cart = $this->logic()->findBywhere([
            'user_id' => USERID,
            'object_id' => $product->product_id,
            'spec_id' => $specsInfo->ps_id,
        ]);

        if (is_null($cart)) {
            $this->logic()->insertForModel([
                'user_id' => USERID,
                'seller_id' => $product->user_id,
                'object_type' => 'shop_product',
                'object_id' => $product->product_id,
                'spec_id' => $specsInfo->ps_id,
                'quantity' => $request->quantity
            ]);
        } else {
            if ($specsInfo->ps_buy_limit > 0) {
                if (bcadd($cart->quantity, $request->quantity) > $specsInfo->ps_buy_limit) return fail(624);
            }
            $cart->quantity += $request->quantity;
            $cart->save();
        }

        return success();
    }


    /**
     * 编辑
     * @param CartEditRequest $request
     * @return JsonResponse
     */
    public function edit(CartEditRequest $request): JsonResponse
    {
        $cart = CartLogic::findBySerialNumber($request);
        if (is_null($cart)) return fail(433);
        $specsInfo = ProductSpecsLogic::findByWhere(['ps_id' => $cart->spec_id]);
        if ($specsInfo->ps_is_delete === 1) return fail(433);
        if ($specsInfo->ps_usable_stock < $request->quantity) return fail(625);
        //目标商品过往购买数量
        $orderCount = ShopOrderLogic::countBoughtProduct($cart->object_id, $cart->spec_id);
        if ($specsInfo->ps_buy_limit > 0) {
            if ($specsInfo->ps_buy_limit < bcadd($orderCount, $request->quantity)) return fail(624);
        }
        $cart->quantity = $request->quantity;
        $cart->save();
        return success();
    }

    /**
     * 删除
     * @param SerialNumbersRequest $request
     * @return JsonResponse
     */
    public function delete(SerialNumbersRequest $request): JsonResponse
    {
        $builder = $this->logic()->query()->whereIn('serial_number', $request->serialNumberArr);
        if ($builder->count() !== count($request->serialNumberArr)) return fail(433);
        $builder->delete();
        return success();
    }

    /**
     * 购物车商品数量
     * @return JsonResponse
     *
     */
    public function count(): JsonResponse
    {
        return success($this->logic()->query()->where('user_id', USERID)->count());
    }
}
